/*
 * ttbqt - A teletekst browser
 * http://code.google.com/p/ttbqt
 * Copyright (C) 2011, Kevin van der Vlist
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <QtDebug>

#include "PageInfo.h"

/**
 * \class PageInfo
 *
 * \short Contains all info related to the teletext state. 
 *
 * This class contains all info regarding the state of ttbqt. 
 */

/**
 * \short Constructor of the PageInfo class. 
 *
 * \param p The pointer to the PageDisplay
 * 
 * Initialise the default values
 */

PageInfo::PageInfo(PageDisplay *p) {
  factory = new Factory(this);
  channel = factory->getTeletextChannel("NOSTeletext");
  page = MIN_PAGE_RANGE;
  subpage = MIN_SUB_PAGE_RANGE;
  pixpage = NULL;
  pagedisplay = p;
}

/**
 * \short Destructor of the page info class
 *
 * Total destruction
 */

PageInfo::~PageInfo() {
  if(factory != NULL) {
    delete factory;
  }
}

/**
 * \short Return the current page id. 
 * 
 * \return int The page id
 *
 * This function is used to retrieve the current page id.
 */

int PageInfo::getPage() {
  return page;
}

/**
 * \short Set the current page id. 
 * 
 * \param p The page id to set
 * 
 * This function is used to set the current page id.
 */

void PageInfo::setPage(int p) {
  if(p >= MIN_PAGE_RANGE && p <= MAX_PAGE_RANGE) {
    if(p != page) {
      qDebug() << "PageInfo::setPage(" << p << ") - valid;";
      page = p;
      subpage = MIN_SUB_PAGE_RANGE;
      channel->getTextPage(page, subpage);
    }
    return;
  }
  qDebug() << "PageInfo::setPage(" << p << ") - INVALID - keeping " << page << ";";
}

/**
 * \short Return the current sub page id. 
 * 
 * \return int The sub page id
 *
 * This function is used to retrieve the current sub page id.
 */

int PageInfo::getSubPage() {
  return subpage;
}

/**
 * \short Set the current sub page id. 
 * 
 * \param p The sub page id to set
 * 
 * This function is used to set the current sub page id.
 */

void PageInfo::setSubPage(int p) {
  if(p >= MIN_SUB_PAGE_RANGE && p <= MAX_SUB_PAGE_RANGE) {
    if(p != subpage) {
      qDebug() << "PageInfo::setSubPage(" << p << ") - valid;";
      subpage = p;
      channel->getTextPage(page, subpage);
    }
    return;
  }
  qDebug() << "PageInfo::setSubPage(" << p << ") - INVALID - keeping " << subpage << ";";
}

void PageInfo::init() {
  channel->getTextPage(page, subpage);
}

/**
 * \short Update the view with the new pixmap
 * 
 * Update the view with the new pixmap
 */

void PageInfo::updatePageDisplay() {
  if(pixpage != NULL) {
    pagedisplay->update(pixpage);
  }
}

/**
 * \short Set the new teletext page
 * 
 * \param p Pointer to the pixmap of the textpage
 * 
 * Set a new pixmap representing a teletekst page
 */

void PageInfo::setTeletextPage(QPixmap *p) {
  if(pixpage != NULL) {
    delete pixpage;
  }
  pixpage = p;
  qDebug() << "PageInfo::setTeleTextPage();";
  updatePageDisplay();
}

/**
 * \short Return the channel map.
 * 
 * \return std::map with all channels 
 *
 * Return the complete channel map.
 */

std::map<QString, Teletext *> *PageInfo::getChannelMap() {
  return factory->getChannelMap();
}

void PageInfo::setTeletextChannel(const QString& value) {
  channel = factory->getTeletextChannel(value);
  page = 1;
  setPage(MIN_PAGE_RANGE);
}

const QString PageInfo::getTeletextChannel() {
  return channel->getChannelName();
}
